<template>
  <div class="dialogContentBox">
    <el-form ref="form" :model="record" label-width="80px" label-position="left" :rules="rules" style="margin:0 20px;">
      <el-form-item :label="$t('名称')" prop="name">
        <el-input v-emoji v-model="record.name" ref="name" size='small'></el-input>
      </el-form-item>
      <el-form-item :label="$t('描述')" prop="desc">
        <el-input v-emoji v-model="record.desc" ref="desc" size='small' type="textarea" @keydown.native="handleKeyCode($event)" :autosize="{ minRows: 2, maxRows: 4 }"></el-input>
      </el-form-item>
      <el-button type="success" size="small" @click="addAsset">{{$t('添加资产对象')}}</el-button>

      <el-table size='mini' :data="memberList" stripe border style="width: 100%;margin-top: 5px;">
        <el-table-column prop='name' :label="$t('名称')" align="center" min-width="100">
          <template slot-scope="scope">
            <tooltip :copyText="scope.row" class="wid190">
            </tooltip>
          </template>
        </el-table-column>
        <el-table-column fixed="right" :label="$t('操作')" width="100" align="center">
          <template slot-scope="scope">
            <el-button @click.native.prevent="deleteRow(scope.$index, record.member_id, memberList)" type="danger" plain size="mini" :class="'del' + (scope.$index + 1)">
              {{$t('删除')}}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <asset-dialog ref="refAssetDialogVisible" @addAssetForTable='addAssetForTable'></asset-dialog>
    </el-form>
  </div>
</template>
<script>
import AssetDialog from './AssetDialog'
import { inputClass, texttextClass } from '@/utils/input-class.js'
export default {
  name: 'AssetGroupDialog',
  components: {
    AssetDialog
  },
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }

    return {
      record: {
        id: null,
        name: '',
        member_id: [],
        desc: ''
      },
      recordCopy: {
        id: null,
        name: '',
        member_id: [],
        desc: ''
      },
      memberList: [],
      rules: {
        name: [{ required: false, validator: validateName, trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      const data = ['name']
      const textdata = ['desc']
      texttextClass(this.$refs, textdata)
      inputClass(this.$refs, data)
    })
  },
  watch: {
    'record.desc': {
      handler (newVal) {
        this.record.desc = newVal.replace(/[\r\n]/g, '')
      },
      immediate: true
    }
  },
  methods: {
    sendCommand () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.record.member_id && this.record.member_id.length < 1) {
            this.$message({
              message: this.$t('资产对象不能为空'),
              type: 'error'
            })
            return
          }
          this.$emit('childForm', this.record, this.record.name)
        }
      })
    },
    closeDialog () {
      this.memberList = []
      this.record = Object.assign({}, this.recordCopy)
      this.$refs.form.resetFields()
    },
    addAsset () {
      this.$nextTick(() => {
        this.$refs.refAssetDialogVisible.init()
      })
    },
    addAssetForTable (data) {
      for (let item of data) {
        if (!this.record.member_id.includes(item.id)) {
          this.record.member_id.push(item.id)
          this.memberList.push(item.name)
        }
      }
    },
    deleteRow (index, rows, rowsList) {
      rows.splice(index, 1)
      rowsList.splice(index, 1)
    },
    handleKeyCode (event) {
      if (event.keyCode === 13) {
        event.preventDefault() // 阻止浏览器默认换行操作
        return false
      }
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(80vh - 184px);
  overflow-y: auto;
}
</style>
